package com.markix.configuration;


import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * @author markix
 * @date 2022/8/27 21:55
 */
public class ClientPkceTest {

    public static void main(String[] args) throws Exception {
        // 明文
        String code_verifier = "xx123";
        // 摘要算法
        String code_challenge_method = "SHA-256";
        // 密文
        String code_challenge = calc(code_verifier, code_challenge_method);
        System.out.println(code_challenge);
        // 3P6kopgvD5SwlNXyxCW-1DnPGJSNGGYn3H0vos0Xu4o
    }

    /**
     * 明文 + 摘要算法，生成 密文
     * @see CodeVerifierAuthenticator#codeVerifierValid(java.lang.String, java.lang.String, java.lang.String)
     */
    private static String calc(String code_verifier, String code_challenge_method) throws Exception {
        byte[] bytes = code_verifier.getBytes(StandardCharsets.US_ASCII);
        MessageDigest md = MessageDigest.getInstance(code_challenge_method);
        byte[] digest = md.digest(bytes);
        String code_challenge = Base64.getUrlEncoder().withoutPadding().encodeToString(digest);
        return code_challenge;
    }

}
